import Vue from 'vue'
export const addUrlOptionsStr = (path, options) => {
  return path
}
const IMAGE_STYLES = {
  article: true,
  home_b: true,
  goods_thumb_1_1: true,
  goods_thumb_3_4: true,
  discount: true,
  national_flag: true
}
const imgPrefixUrl = function (path, options) {
  const baseUrl = process.env.apiStaticURL
  // 处理base64图片
  if (path?.includes(';base64')) {
    return path
  }
  // 处理图片路径
  if (path && !path.includes('://')) {
    path = baseUrl + path
  }
  // 非阿里云图片不处理
  if (!path?.includes(baseUrl)) {
    return path
  }
  if (!options) {
    return path
  }
  // 处理预设的图片样式
  if (IMAGE_STYLES[options]) {
    return `${path}?x-oss-process=style/${options}`
  }
  // 处理不同类型的options
  switch (typeof options) {
    case 'string':
      // 处理宽高比配置 "width:height"
      if (options.includes(':')) {
        const [width, height] = options.split(':').map(num => parseInt(num, 10))
        return `${path}?x-oss-process=image/resize,m_fill,limit_0,w_${width},h_${height}`
      }
      return `${path}?x-oss-process=image/${options}`
    case 'number':
      // 等比缩放
      return `${path}?x-oss-process=image/resize,m_fill,limit_0,w_${options},h_${options}`
    case 'function':
      // 自定义处理函数
      return `${path}?x-oss-process=image/${options(path)}`
    case 'object':
      // 处理配置对象
      const { type = 'resize', ...params } = options
      const process = Object.entries(params)
        .map(([key, value]) => `${key}_${value}`)
        .join(',')
      return `${path}?x-oss-process=image/${type},${process}`
    default:
      return path
  }
}
const tools = {
  install(Vue) {
    Vue.prototype.$globalFn = {
      imgPrefixUrl
    }
    //Vue.prototype.$isMoible=isMoible()
  }
}
Vue.use(tools)
